package com.koocloud.electroplatemanage.service.impl;

import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.*;
import com.koocloud.electroplatemanage.pojo.*;
import com.koocloud.electroplatemanage.pojo.Process;
import com.koocloud.electroplatemanage.service.CraftCardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @program:
 * @description:
 * @author: wjx
 * @create: 2020-11-25 10:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CraftCardServiceImpl implements CraftCardService {

    @Resource
    CraftCardMapper craftCardMapper;

    @Resource
    ProcessCheckMapper processCheckMapper;

    @Resource
    ProcessMapper processMapper;

    @Resource
    WorkProcessMapper workProcessMapper;

    @Resource
    IncomeBasicInfoMapper incomeBasicInfoMapper;

    @Override
    public ResponseTemplate selectByWorkOrderNumber(String workOrderNumber) {
        try {
            ProcessCheck processCheck = processCheckMapper.selectByWorkOrderNumber(workOrderNumber);
            Process process = processMapper.selectByWorkOrderNumber(workOrderNumber);
            WorkProcessVo workProcessVo = workProcessMapper.selectByWorkOrderNumber(workOrderNumber);
            IncomeBasicInfoVo incomeBasicInfoVo = incomeBasicInfoMapper.selectIncomeBasicInfo(workOrderNumber);

            if (!StringUtils.isEmpty(processCheck)||!StringUtils.isEmpty(incomeBasicInfoVo)||!StringUtils.isEmpty(processCheck)||!StringUtils.isEmpty(workProcessVo)){
                CraftCard craftCard = new CraftCard();
                craftCard.setProcess(process);
                craftCard.setIncomeBasicInfoVo(incomeBasicInfoVo);
                craftCard.setProcessCheck(processCheck);
                craftCard.setWorkProcessVo(workProcessVo);
                return ResponseTemplate.builder().code(0).message("查询成功").count(0l).data(craftCard).build();
            } else {
                //没有查询到数据
                return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
}
